<?php
namespace app\common\model;




use think\Model;

class UserFriend extends Model
{

    public function user()
    {
        return  $this->belongsTo('users', 'user_id','user_id')->field('nick_name,header_img')->setEagerlyType(0);
    }


    protected function getUserIdAttr($value, $data)
    {
        return hashid($data['user_id'] ?? 0);
    }

    protected function getFollowUserAttr($value, $data)
    {
        return hashid($data['follow_user'] ?? 0);
    }

    public function getRows($map = [],$thisUser = 0)
    {

        $rows =  $this->alias('a')
            ->join('user_extend e','e.user_id = a.user_id','LEFT')
            ->join('users u','u.user_id = a.user_id','LEFT')
            ->where($map)
            ->field('u.user_id,u.nick_name,u.header_img,u.uuid')
            ->order('a.update_time desc')
            ->paginate()
            ->each(function ($item)use($thisUser){
                if ($thisUser == 0){
                    $item['is_black'] = 0;
                }else{
                    $item['is_black'] = 2 == \app\api\controller\Base::followStatus($thisUser,$item->data['user_id'])?1:0;
                }


//                if ($item['online_status']){
//                    $item['status'] = '当前在线';
//                }else{
//                    $item['status'] = formatTime($item['online_time']);
//                }
//                $item['user_id'] = hashid($item['user_id']);
            });

        return ['thisPage'=>$rows->currentPage(),'hasNext'=>$rows->hasMore(),'data'=>$rows->items()];

    }

    public static function search($map = [])
    {

        return  self::alias('a')
//            ->join('user_extend e','e.user_id = a.user_id','LEFT')
            ->join('users u','u.user_id = a.user_id','LEFT')
            ->where($map)
            ->field('u.user_id,u.nick_name,u.header_img,u.uuid,u.sign')
            ->select();


    }
}